package com.jinlejia.bbs.ui.gambit;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jinlejia.bbs.R;
import com.jinlejia.bbs.adapter.GambitAdapter;
import com.jinlejia.bbs.bean.GambitListBean;
import com.jinlejia.bbs.databinding.BbsFragmentGambitAttentionBinding;
import com.jinlejia.bbs.logic.GambitAttentionLogic;
import com.jinlejia.bbs.ui.fun.BBSAskActivity;
import com.jinlejia.bbs.ui.fun.BBSSearchActivity;
import com.jinlejia.bbs.widget.mp3.AudioController;
import com.jinlejia.bbs.widget.recycler.PullInterface;

/**
 * 话题-关注标签页
 */
public class BBSGambitAttentionFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener, PullInterface.LoadMoreListener, GambitAttentionLogic.LogicCallBack {

    private BbsFragmentGambitAttentionBinding binding;
    private View view;
    private GambitListBean data = new GambitListBean();
    private GambitAttentionLogic logic;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view==null){
            view = inflater.inflate(R.layout.bbs_fragment_gambit_attention, container, false);
            binding = DataBindingUtil.bind(view);
        }
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }
        if (logic==null){
            logic = new GambitAttentionLogic(this,data,this);
        }

        return view;
    }
    private AudioController audioController;
    @Override
    public void onResume() {
        super.onResume();
        initView();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        audioController = new AudioController(getContext());
    }

    @Override
    public void onDetach() {
        if (audioController!=null){
            audioController.release();
        }
        super.onDetach();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    private GambitAdapter adapter;
    private void initView() {
        binding.askButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getContext(), BBSAskActivity.class);
                startActivityForResult(intent,askRequestId);
            }
        });
        binding.searchEditText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getContext(), BBSSearchActivity.class);
                startActivityForResult(intent,searchRequestId);
            }
        });
        if (adapter==null){
            adapter = new GambitAdapter(getContext(),audioController,binding.recyclerView);
            LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
            binding.recyclerView.setAdapter(adapter);
            binding.recyclerView.setLayoutManager(layoutManager);
            binding.recyclerView.setAutoLoadMoreListener(this);
            binding.swipeRefreshLayout.setOnRefreshListener(this);
        }
        binding.swipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                binding.swipeRefreshLayout.setRefreshing(true);
                String searchText = binding.searchEditText.getText().toString();
                logic.refresh(searchText);
            }
        });
    }

    @Override
    public void onRefresh() {
        binding.swipeRefreshLayout.setRefreshing(true);
        String searchText = binding.searchEditText.getText().toString();
        logic.refresh(searchText);
    }

    @Override
    public void onLoadMore() {
        binding.swipeRefreshLayout.setRefreshing(true);
        String searchText = binding.searchEditText.getText().toString();
        logic.load(searchText);
    }

    @Override
    public void onResult(boolean hasNextPage) {
        adapter.initData(data);
        if (!hasNextPage){
            binding.recyclerView.setAutoLoadMoreListener(null);
        }else {
            binding.recyclerView.setAutoLoadMoreListener(this);
        }
        binding.swipeRefreshLayout.setRefreshing(false);
        binding.recyclerView.getAdapter().notifyDataSetChanged();
    }
    private int searchRequestId = 10;
    private int askRequestId = 11;
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode==searchRequestId && resultCode == Activity.RESULT_OK){ //搜索
            String name = data.getStringExtra("name");
            searchInit(name);
        }else if (requestCode==askRequestId && resultCode == Activity.RESULT_OK){
            onRefresh();
        }
    }

    private void searchInit(final String name) {
        binding.searchEditText.setText(name);
        onRefresh();
    }
}
